package cn.bdqn.controller;

import cn.bdqn.biz.IUserBiz;
import cn.bdqn.pojo.Role;
import cn.bdqn.pojo.User;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private IUserBiz userbiz;
    @RequestMapping("/")
    public String loginIndex() {
        return "redirect:/WEb-INF/login.jsp";
    }



    @RequestMapping("/login")
    public String userLogin(User user, HttpSession session) {
        System.out.println("进入方法");
        User u = userbiz.login(user);
        System.out.println(u + "=========");
        if (u != null) {
            session.setAttribute("userSession", u);
            return "/jsp/frame";
        }

        return "/login";
    }

//    @RequestMapping("/pwdmodify")
//    public String jwdmdify() {
//        System.out.println("进入方法");
//        return "/jsp/pwdmodify";
//    }

    @RequestMapping("/modify")
    public String modifyPwd() {



    return  "jsp/pwdmodify";
    }
    @RequestMapping("/comfirmpwd")
    @ResponseBody
public String comfirmpwd(String oldpassword,HttpSession session){
   User user=(User)session.getAttribute("userSession");
   Map map=  new HashMap();
        if(oldpassword.equals(user.getUserPassword())){
            map.put("result","true");
   }else if (user == null){
       map.put("result","sessionerror");
   } else if (oldpassword==""||oldpassword==null){
            map.put("result","error");
   }else {
    map.put("result","false");
        }
        String result= JSON.toJSONString(map);
        System.out.println(result);
        return result;
}
    @RequestMapping("/userpwdmodify")
    public String userpwdmodify(@RequestParam("newpassword") String password,HttpSession session){
User user=(User)session.getAttribute("userSession");
try {
    userbiz.modifyPassword(user.getId(), password);
    return "/login";
}catch (Exception e){

return "redirect:/500.html";
}

    }
    @RequestMapping(value="/userManager",produces = "text/json;charset=UTF-8")
    public String userManager(Integer pageIndex, Integer pageSize, Model model, String queryname, Integer queryUserRole){

     Page<User> userPage = userbiz.findUserBypage(pageIndex, pageSize,queryname,queryUserRole);
     model.addAttribute("userPage", userPage);
if (queryname!=null&&  !"".equals(queryname)){
model.addAttribute("queryName",queryname);
}
if (queryUserRole!=null && queryUserRole!=0){
model.addAttribute("queryUserRole",queryUserRole);
}
        return "forward:getRoleList.do";
    }
    @RequestMapping("/useradd")
    public String useradd() {



        return  "jsp/useradd";
    }
    @RequestMapping("/userss")
    @ResponseBody
    public String userRole(String userCode,HttpSession session) {

        Map map=new HashMap();

        System.out.println(map);
        String result= JSON.toJSONString(map);
        System.out.println(result);
        return result;
    }
}

